﻿using RRQMMVVM;
using System.Windows;
using System.Windows.Media;

namespace ViewModel.ViewModels
{
    public class WindowBodyViewModel : ViewModelBase
    {
        public WindowBodyViewModel()
        {
            this.RecentlyButtonCommand = new ExecuteCommand(RecentlyButton);
            this.ChatListButtonCommand = new ExecuteCommand(ChatListButton);

            this.RecentlyContactListItems = new RRQMList<RecentlyContactItemViewModel>();
            this.RecentlyContactShowListItems = new RRQMList<RecentlyContactItemViewModel>();
            this.ContactListItems = new RRQMList<ContactItemViewModel>();

            this.FriendListItems = new RRQMList<ContactItemViewModel>();

            RecentlyContactText = string.Empty;
            ContactText = string.Empty;
            UnReadMesNum = 0;
            RecentlyButton();
        }

        private static Brush noSelected = (SolidColorBrush)new BrushConverter().ConvertFromString("#595B5C");
        private static Brush Selected = (SolidColorBrush)new BrushConverter().ConvertFromString("#0086ED");

        #region Command

        public ExecuteCommand RecentlyButtonCommand { get; set; }
        public ExecuteCommand ChatListButtonCommand { get; set; }

        #endregion Command

        #region 属性

        public RRQMList<ContactItemViewModel> FriendListItems;

        private string recentlyContactText;

        /// <summary>
        /// 最近联系人列表搜索框文字
        /// </summary>
        public string RecentlyContactText
        {
            get { return recentlyContactText; }
            set
            {
                recentlyContactText = value;
                this.RecentlyContactShowListItems.Clear();
                foreach (var item in RecentlyContactListItems)
                {
                    if (item.NickName.Contains(value))
                    {
                        this.RecentlyContactShowListItems.Add(item);
                    }
                }

                OnPropertyChanged();
            }
        }

        private string contactText;

        /// <summary>
        /// 联系人列表搜索框文字
        /// </summary>
        public string ContactText
        {
            get { return contactText; }
            set
            {
                contactText = value;
                this.ContactListItems.Clear();
                foreach (var item in FriendListItems)
                {
                    if (item.NickName.Contains(value))
                    {
                        this.ContactListItems.Add(item);
                    }
                }

                OnPropertyChanged();
            }
        }

        private RecentlyContactItemViewModel recentlyListSelectItem = new RecentlyContactItemViewModel();

        /// <summary>
        /// 当前选中的条目
        /// </summary>
        public RecentlyContactItemViewModel RecentlyListSelectItem
        {
            get { return recentlyListSelectItem; }
            set
            {
                recentlyListSelectItem = value;
                if (value != null)
                {
                    this.UnReadMesNum -= value.UnreadNumber;
                    value.UnreadNumber = 0;
                }
                OnPropertyChanged();
            }
        }

        private RRQMList<RecentlyContactItemViewModel> recentlyContactListItems;

        public RRQMList<RecentlyContactItemViewModel> RecentlyContactListItems
        {
            get { return recentlyContactListItems; }
            set { recentlyContactListItems = value; OnPropertyChanged(); }
        }

        private RRQMList<RecentlyContactItemViewModel> recentlyContactShowListItems;

        public RRQMList<RecentlyContactItemViewModel> RecentlyContactShowListItems
        {
            get { return recentlyContactShowListItems; }
            set { recentlyContactShowListItems = value; OnPropertyChanged(); }
        }

        private RRQMList<ContactItemViewModel> contactListItems;

        public RRQMList<ContactItemViewModel> ContactListItems
        {
            get { return contactListItems; }
            set { contactListItems = value; OnPropertyChanged(); }
        }

        private Visibility recentlyChatListVisible;

        public Visibility RecentlyChatListVisible
        {
            get { return recentlyChatListVisible; }
            set { recentlyChatListVisible = value; OnPropertyChanged(); }
        }

        private Visibility chatListVisible;

        public Visibility ChatListVisible
        {
            get { return chatListVisible; }
            set { chatListVisible = value; OnPropertyChanged(); }
        }

        private Brush recentlyTextForegroung = noSelected;

        public Brush RecentlyTextForegroung
        {
            get { return recentlyTextForegroung; }
            set { recentlyTextForegroung = value; OnPropertyChanged(); }
        }

        private Brush listTextForegroung = noSelected;

        public Brush ListTextForegroung
        {
            get { return listTextForegroung; }
            set { listTextForegroung = value; OnPropertyChanged(); }
        }

        private int unReadMesNum;

        public int UnReadMesNum
        {
            get { return unReadMesNum; }
            set
            {
                unReadMesNum = value;
                if (value == 0)
                {
                    this.EllipseVisible = Visibility.Hidden;
                }
                else
                {
                    this.EllipseVisible = Visibility.Visible;
                }

                OnPropertyChanged();
            }
        }

        private Visibility ellipseVisible = Visibility.Hidden;

        public Visibility EllipseVisible
        {
            get { return ellipseVisible; }
            set { ellipseVisible = value; OnPropertyChanged(); }
        }

        #endregion 属性

        #region 绑定方法

        /// <summary>
        /// 选择最近联系人面板
        /// </summary>
        public void RecentlyButton()
        {
            this.RecentlyChatListVisible = Visibility.Visible;
            this.ChatListVisible = Visibility.Hidden;

            this.RecentlyTextForegroung = Selected;
            this.ListTextForegroung = noSelected;
        }

        /// <summary>
        /// 选择联系人面板
        /// </summary>
        public void ChatListButton()
        {
            this.RecentlyChatListVisible = Visibility.Hidden;
            this.ChatListVisible = Visibility.Visible;
            this.RecentlyTextForegroung = noSelected;
            this.ListTextForegroung = Selected;
        }

        #endregion 绑定方法
    }
}